// eslint-disable-next-line
const importFrom = require('import-from')



function getPackages (context) {
  return Promise.resolve()
    .then(() => {
      const ctx = context || {}
      const cwd = ctx.cwd || process.cwd()
      const Project = importFrom(cwd, '@lerna/project')
      const project = new Project(cwd)
      return project.getPackages()
    })
    .then(packages => {
      return packages.map(pkg => pkg.name).map(name => (name.charAt(0) === '@' ? name.split('/')[1] : name))
    })
}

const scopes = [
  'project',
  'core',
  'style',
  'docs',
  'ci',
  'dev',
  'build',
  'deploy',
  'other',
  'typography',
  'color',
]

// git commit -m <type>[optional scope]: <description>
// Level [0..2]: 0 disables the rule. For 1 it will be considered a warning for 2 an error.
// Applicable always|never: never inverts the rule.
// Value: value to use for this rule.
module.exports = {
  rules: {
    'scope-enum': ctx => getPackages(ctx).then(packages => [2, 'always', [...packages, ...scopes]]),
    'body-leading-blank': [1, 'always'],
    'footer-leading-blank': [1, 'always'],
    'header-max-length': [2, 'always', 72],
    'scope-case': [2, 'always', 'lower-case'],
    'subject-case': [1, 'never', ['sentence-case', 'start-case', 'pascal-case', 'upper-case']],
    'subject-empty': [2, 'never'],
    'subject-full-stop': [2, 'never', '.'],
    'type-case': [2, 'always', 'lower-case'],
    'type-empty': [2, 'never'],
    'type-enum': [
      2,
      'always',
      ['build', 'chore', 'ci', 'docs', 'feat', 'fix', 'perf', 'refactor', 'revert', 'style', 'test', 'improvement'],
    ],
  },
}

// type-enum
// build	编译相关的修改，例如发布版本、对项目构建或者依赖的改动
// chore	其他修改, 比如改变构建流程、或者增加依赖库、工具等
// ci	持续集成修改
// docs	文档修改
// feat	新特性、新功能
// fix	修改bug
// perf	优化相关，比如提升性能、体验
// refactor	代码重构
// revert	回滚到上一个版本
// style	代码格式修改, 注意不是 css 修改
// test	测试用例修改
